package com.SMP.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.SMP.model.Msg;
import com.SMP.model.Role;
import com.SMP.service.PermissionService;
import com.SMP.service.RolePermissionService;
import com.SMP.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class RoleConfigController {

	/**
	 * @author WYHY
	 */
	@Autowired
	RoleService roleService;
	@Autowired 
	RolePermissionService rolePermissionService;
	@Autowired
	PermissionService permissionService;
	
	/**
	 * @author WYHY
//	 * @param 添加角色
	 * */
	@RequestMapping("/addRole")
	@ResponseBody
	public Msg addRole(@RequestParam(value="rolename")String rolename,@RequestParam(value="rolecode")String rolecode,@RequestParam(value="permissionid")List permissionid) {
		int a=roleService.addRole(rolename, rolecode);
		System.out.println(rolename);
		System.out.println(rolecode);
		Role role=roleService.selRoleName(rolename);
		System.out.println(role);
		System.out.println(permissionid);
		int b=rolePermissionService.permissionToRole(String.valueOf(role.getId()), permissionid);
		if (a==1) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}
	
	/**
	 * @author WYHY
//	 * @param 删除角色
	 * */
	@RequestMapping("/delRole")
	@ResponseBody
	public Msg delRole(@RequestParam(value="roleid")String roleid) {
		int a=roleService.delRole(roleid);
		if (a==1) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}
	
	/**
	 * @author WYHY
//	 * @param 修改角色
	 * */
	@RequestMapping("/updRole")
	@ResponseBody
	public Msg updRole(@RequestParam(value="roleid")String roleid,@RequestParam(value="rolename")String rolename,@RequestParam(value="rolecode")String rolecode,@RequestParam(value="permissionid")List permissionid) {
		int a=roleService.updRole(roleid, rolename,rolecode);
		int b=rolePermissionService.permissionToRole(roleid, permissionid);
		if (a==1) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}
	
	/**
	 * @author WYHY
//	 * @param 查询所有角色
	 * */
	@RequestMapping("/selAllRole")
	@ResponseBody
	public Msg selAllRole(@RequestParam(value = "pn") Integer pn) {
		PageHelper.startPage(pn, 10);
		List<Role> list=roleService.selAllRole();
		PageInfo<Role> page = new PageInfo<Role>(list,  5);
		return Msg.success().add("pageInfo", page);
	}
	
	/**
	 * @author WYHY
//	 * @param 根据姓名查询角色
	 * */
	@RequestMapping("/selRoleM")
	@ResponseBody
	public Role selRole(@RequestParam(value="rolename")String rolename) {
		Role role=roleService.selRoleName(rolename);
		return role;
	}
	
	/**
	 * @author WYHY
//	 * @param 查询角色列表
	 * */
	@RequestMapping("/selRoleA")
	@ResponseBody
	public Map selRoleA(@RequestParam(value="roleid")String roleid) {
		Role role=roleService.selRole(roleid);
		List list=rolePermissionService.roleSelPermission(roleid);
		Map map=new HashMap();
		map.put("manager", role);
		map.put("Permissions", list);
		return map;
	}
	
	/**
	 * @author WYHY
//	 * @param 权限列表
	 * */
	@RequestMapping("/selPermission")
	@ResponseBody
	public List selTree() {
		List list=permissionService.selTree();
		return list;
	}
}
